﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// TMS运单信息查询
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ShippingorderInformationController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public ShippingorderInformationController(ILogger<TMSShipppingorderRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration,ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");
            //_logger = loggerFactory.CreateLogger<IWMSTokenCheckService>();

        }

        /// <summary>
        /// TMS运单信息查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostShippingorderInformationSelect([FromBody] TMSShipppingorderRequestDto strparm)
        {
            string  strMsg = "";
            //_logger.LogInformation(JsonConvert.SerializeObject(strparm));
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            TMSShipppingorderRequestDto result = new TMSShipppingorderRequestDto();
            TMSShipppingorderResponseDto re = new TMSShipppingorderResponseDto();
            string ccode = strparm.ccode;
            string ddate1 = strparm.ddate1;
            string ddate2 = strparm.ddate2;
            string carno = strparm.carno;
            string sales_order_id = strparm.sales_order_id;
            string sales_order_line_id = strparm.sales_order_line_id;
            string factory_id = strparm.factory_id;

            //框架查询方法
            //var data = _WMSTokenCheckService.GetTokenCheckData(supplier_code);

            try
            {
                strSql = new StringBuilder();
                strSql.Append(@"select ma.factory_code,ma.factory_id,ma.factory_name,ma.ccode,ma.ddate,ma.creation_date,ma.drivername,ma.identityno,ma.telephone,ma.carno2,ma.carriername,ma.carrier_dispatch_no,ma.carriercode,de.sales_order_no,de.sales_order_id,de.customer_code,de.customer_name,convert(de.sales_order_line_id,char(100)) sales_order_line_id,de.sales_order_line_no
,de.material_code,de.material_name,de.material_unit,de.material_amount,de.shipment_amount,de.start_address	,
de.target_address,de.receipt_man,de.receipt_tel,de.receipt_no,de.shippingorderno,de.sales_contract_no,ma.trans_type,ma.is_inner_order,de.customer_remarks,pl.planid from
shippingorder_main ma left join shippingorder_sales_details de on ma.document_id=de.tab_doc_id left join (select DISTINCT planid,shippingorderno from shippingorder_plan_details)pl on ma.ccode=pl.shippingorderno   where de.shippingorderno is not null and (ma.is_end is null or ma.is_end<>1)  ");

                if (factory_id.Trim() != "")
                {
                    strSql.Append(" and ma.factory_id='" + factory_id.Trim() + "'");
                }
                if (ccode.Trim() != "")
                {
                    strSql.Append(" and ma.ccode='" + ccode.Trim() + "'");
                }
                if (ddate1.Trim() != "")
                {
                    strSql.Append(" and ma.ddate>='" + ddate1.Trim() + "'");
                }
                if (ddate2.Trim() != "")
                {
                    strSql.Append(" and ma.ddate<='" + ddate2.Trim() + "'");
                }
                if (carno.Trim() != "")
                {
                    strSql.Append(" and ma.carno2='" + carno.Trim() + "'");
                }
                if (sales_order_id.Trim() != "")
                {
                    strSql.Append(" and de.sales_order_id='" + sales_order_id.Trim() + "'");
                }
                if (sales_order_line_id.Trim() != "")
                {
                    strSql.Append(" and de.sales_order_line_id=" + sales_order_line_id.Trim() + "");
                }

                DataTable dt_Shippingorder = DB.Query(strSql.ToString(), con);
                if (dt_Shippingorder.Rows.Count<=0)
                {
                    strMsg = "TMS不存在运单号信息，请检查清楚再调用接口";
                }
                else
                {
                    re.data = DB.TableToListModel<TMSShipppingorderResponseData>(dt_Shippingorder);
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "1";
                re.message = "";
            }
            else
            {
                re.status = "失败";
                re.code = "0";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }





    }
}
